﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace Axe.Imaging
{
    public class ColorTransform
    {
        public float Aa = 1.0f;
        public float Ra = 1.0f;
        public float Ga = 1.0f;
        public float Ba = 1.0f;

        public int Ab = 0;
        public int Rb = 0;
        public int Gb = 0;
        public int Bb = 0;

        private byte RoundByteColor(int color)
        {
            int val = Math.Max(color, 0);
            val = Math.Min(val, 255);
            return (byte)val;
        }

        private byte RoundByteColor(float color)
        {
            return RoundByteColor((int)color);
        }

        public void SetClamp(Color c)
        {
            Ab = c.A;
            Rb = c.R;
            Gb = c.G;
            Bb = c.B;
        }

        public Color ApplyTo(Color c)
        {
            //return Color.FromArgb(c.A,c.R,c.G,c.B);
            
            byte alpha = RoundByteColor(c.A * Aa + Ab);
            byte r, g, b;
            
            if (c.A  > 0)
            {
                alpha = c.A;
                r = RoundByteColor(c.R * Ra + Rb);
                g = RoundByteColor(c.G * Ga + Gb);
                b = RoundByteColor(c.B * Ba + Bb);
            }
            else
            {
                r = c.R; g = c.G; b = c.B;
                alpha = 0;
            }

            Color newColor = Color.FromArgb(alpha, r, g, b);

            return newColor;
        }
    }
}
